{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "d68cd136",
   "metadata": {
    "pycharm": {
     "is_executing": true
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "本机IP地址为：222.89.167.31\n"
     ]
    }
   ],
   "source": [
    "import requests\n",
    "import re\n",
    "\n",
    "r = requests.get(\"http://txt.go.sohu.com/ip/soip\")\n",
    "\n",
    "ip = re.findall(r'\\d+.\\d+.\\d+.\\d+',r.text)\n",
    "\n",
    "print(f\"本机IP地址为：{ip[0]}\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "dcf7a3ae",
   "metadata": {
    "pycharm": {
     "is_executing": true
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "IP地址所在地为河南省许昌市\n",
      "所在地的经纬度为为东经113.85848度，北纬34.04143度\n"
     ]
    }
   ],
   "source": [
    "import json\n",
    "\n",
    "def get_city(add):\n",
    "    url = 'https://api.map.baidu.com/location/ip'\n",
    "    params = { 'ip' : add,           \n",
    "               'ak' : 'vDdmqBlRqXZEDRTEb7lxX6O0cESsPfWI',\n",
    "#                 'sn' : 'BqmCSZZaNfVrHKgb7B6Gydk6uGgKu6om', # 百度密钥\n",
    "               'coor': 'bd09ll'     }          # 输出结果设置为json格式\n",
    "    res = requests.get(url,params)\n",
    "    jd = json.loads(res.text)                 # 将json格式转化为Python字典\n",
    "    return jd\n",
    "\n",
    "address = get_city(ip)\n",
    "position = address['content']['address']\n",
    "print(f'IP地址所在地为{position}')\n",
    "lat = address['content']['point']['y']\n",
    "lng = address['content']['point']['x']\n",
    "print(f\"所在地的经纬度为为东经{float(lng):8.5f}度，北纬{float(lat):8.5f}度\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "88e266e0",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "\n",
      "当前时间为:\n",
      "2023-02-09 15:51:19.737152\n",
      "太阳高度角为：23:08:07.1, 方位角为：231:33:53.4。\n"
     ]
    }
   ],
   "source": [
    "import ephem\n",
    "import datetime\n",
    "\n",
    "observer = ephem.Observer()\n",
    "observer.lat, observer.lon = str(lat), str(lng)\n",
    "\n",
    "# observer.date = ephem.Date('2021/12/24 02:33:00.00')  # 手动指定时间(UTC时间)，也可以注释该行，反注释下面两行，自动获取当前时间进行计算\n",
    "observer.date = datetime.datetime.utcnow()    # 计算当前时间\n",
    "print(\"\\n当前时间为:\\n%s\" % ephem.localtime(observer.date))\n",
    "\n",
    "sun = ephem.Sun()\n",
    "sun.compute(observer)\n",
    "print(f\"太阳高度角为：{sun.alt}, 方位角为：{sun.az}。\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "c8c11260",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "2.3404682728867323\n"
     ]
    }
   ],
   "source": [
    "import numpy as np\n",
    "\n",
    "ratio = 1 / np.tan(sun.alt)\n",
    "print(ratio)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "84644281",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "172.18.0.152\n"
     ]
    }
   ],
   "source": [
    "import socket\n",
    "\n",
    "def get_host_ip():\n",
    "    \"\"\"\n",
    "    查询本机局域网ip地址\n",
    "    :return:\n",
    "    \"\"\"\n",
    "    try:\n",
    "        s=socket.socket(socket.AF_INET,socket.SOCK_DGRAM)\n",
    "        s.connect(('8.8.8.8',80))\n",
    "        ip=s.getsockname()[0]\n",
    "    finally:\n",
    "        s.close()\n",
    "    return ip\n",
    "\n",
    "\n",
    "if __name__ == '__main__':\n",
    "    print(get_host_ip())"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "3942c9df",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "222.89.167.31\n"
     ]
    }
   ],
   "source": [
    "import requests\n",
    "import re\n",
    "\n",
    "r = requests.get(\"http://txt.go.sohu.com/ip/soip\")\n",
    "\n",
    "ip = re.findall(r'\\d+.\\d+.\\d+.\\d+',r.text)\n",
    "\n",
    "print(ip[0])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "id": "4bd2d118",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "随州市曾都区秋实佳苑小区的经度为东经113.48696度, 纬度为北纬31.69157度\n"
     ]
    }
   ],
   "source": [
    "import requests\n",
    "import pandas as pd\n",
    "import json\n",
    "\n",
    "def getlnglat(add):\n",
    "    url = 'https://api.map.baidu.com/geocoding/v3/'\n",
    "    params = { 'address' : add,           \n",
    "               'ak' : 'vDdmqBlRqXZEDRTEb7lxX6O0cESsPfWI',\n",
    "#                 'sn' : 'BqmCSZZaNfVrHKgb7B6Gydk6uGgKu6om', # 百度密钥\n",
    "               'output': 'json'     }          # 输出结果设置为json格式\n",
    "    res = requests.get(url,params)\n",
    "    jd = json.loads(res.text)                 # 将json格式转化为Python字典\n",
    "    return jd\n",
    "\n",
    "address = '随州市曾都区秋实佳苑小区'\n",
    "lnglat = getlnglat(address)\n",
    "lat = lnglat['result']['location']['lat']\n",
    "lng = lnglat['result']['location']['lng']\n",
    "print(f\"{address}的经度为东经{lng:8.5f}度, 纬度为北纬{lat:8.5f}度\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "id": "118fa620",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'status': 0,\n",
       " 'result': {'location': {'lng': 113.486959015227, 'lat': 31.691574916479663},\n",
       "  'precise': 1,\n",
       "  'confidence': 70,\n",
       "  'comprehension': 100,\n",
       "  'level': '地产小区'}}"
      ]
     },
     "execution_count": 8,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "lnglat"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "70ba2d9b",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "870b96c8",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.9.7"
  },
  "latex_envs": {
   "LaTeX_envs_menu_present": true,
   "autoclose": false,
   "autocomplete": true,
   "bibliofile": "biblio.bib",
   "cite_by": "apalike",
   "current_citInitial": 1,
   "eqLabelWithNumbers": true,
   "eqNumInitial": 1,
   "hotkeys": {
    "equation": "Ctrl-E",
    "itemize": "Ctrl-I"
   },
   "labels_anchors": false,
   "latex_user_defs": false,
   "report_style_numbering": false,
   "user_envs_cfg": false
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
